package vcp.server.requesthandler;

import ocsf.server.ConnectionToClient;
import vcp.client.request.CustomerRegularCheckInRequest;
import vcp.client.request.response.ResponseRequest;
import vcp.entity.Car;
import vcp.entity.Customer;
import vcp.entity.Lot;
import vcp.entity.Site;
import vcp.entity.Subscription;
import vcp.entity.User;
import vcp.entity.utility.RobotManager;
import vcp.enums.LotStatus;
import vcp.enums.UserType;
import vcp.server.DAO.CarDAO;
import vcp.server.DAO.CustomerDAO;
import vcp.server.DAO.LotDAO;
import vcp.server.DAO.SiteDAO;
import vcp.server.DAO.SubscriptionDAO;
import vcp.server.DAO.UserDAO;
import vcp.server.interfaces.IServer;
import vcp.server.utility.SystemClock;
/**
 * 
 * @author VCP team
 *<p>
 *class handles requests related to regular customer check in
 *</p>
 */
public class CustomerRegularCheckInRequestHandler extends RequestHandler <CustomerRegularCheckInRequest>
{
	/**
	 * class constructor
	 * @param server
	 */
	public CustomerRegularCheckInRequestHandler ( IServer server )
	{
		super(server);
	}

	/****************************************************************************************************************************
	 *** CommandHandler methods ***
	 ****************************************************************************************************************************/
	@Override
	public void execute ( CustomerRegularCheckInRequest request, ConnectionToClient client )
	{
		UserDAO userDao = new UserDAO();
		CustomerDAO customerDao = new CustomerDAO();
		SubscriptionDAO subscriptionDao = new SubscriptionDAO();
		CarDAO carDao = new CarDAO();
		LotDAO lotDAO = new LotDAO();

		User user = userDao.findByID(request.getSubscription().getCustomerID());
		if ( user == null )
		{
			user = userDao.create();
			user.setUserID(request.getSubscription().getCustomerID());
			user.setUserFirstName("One Time Use Customer");
			user.setUserLastName("Unknow");
			user.setPassword("1");
			user.setUserType(UserType.CUSTOMER);
			user.setUserEmail(request.getEmail());
			user.setLoggedIn(false);
			userDao.save(user);

			Customer customer = customerDao.create();
			customer.setCustomerID(request.getSubscription().getCustomerID());
			customer.setBussinessClass(1);
			customer.setPhoneNo("000000");
			customer.setBalance(0);
			customerDao.save(customer);
		}

		Subscription subscription = request.getSubscription();
		subscription.setArrivalDate(SystemClock.getClockInstance().getSystemDateTime());
		subscription.setEstimatedLeaveDate(request.getEstimatedLeaveDate());
		subscriptionDao.save(subscription);

		Car car = subscription.getCars().get(0);
		car.setParkedOnSite(true);
		car.setArrivedTime(SystemClock.getClockInstance().getSystemDateTime());
		carDao.save(car);

		Lot lot = RobotManager
				.getRobotManagerInstance()
				.getSiteRobot(subscription.getSite())
				.getAvailableLot(subscription);

		lot.setLotStatus(LotStatus.OCCUPIED);
		lot.setCarID(car.getCarID());
		lot.setReservedBySubscription(subscription.getSubscribtionID());

		lotDAO.updateLotStatusByID(subscription.getSite().getSiteID(), lot.getFloor(), lot.getRow(), lot.getColumn(), lot.getLotStatus());
		lotDAO.updateCarInLot(subscription.getSite().getSiteID(), lot.getFloor(), lot.getRow(), lot.getColumn(), lot.getCarID());
		lotDAO.updateReservedBySubscription(subscription.getSite().getSiteID(), lot.getFloor(), lot.getRow(), lot.getColumn(), lot.getReservedBySubscription());

		SiteDAO siteDAO = new SiteDAO();
		Site site = siteDAO.findByID(subscription.getSite().getSiteID());

		site.setOccupiedLots(site.getOccupiedLots() + 1);
		siteDAO.updateOccupiedLotsByID(site.getSiteID(), site.getOccupiedLots());

		siteDAO.getSiteLotsOnDemand(site);

		this.sendResponseRefresh(site, client);
		this.sendResponse(new ResponseRequest(request.getRequestID(), new Boolean(true)), client);
	}
}
